package marksys.free.com.marksys.ui.face.facebykomlinplus;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;


public class FaceHelper {
    private static final String TAG = "FaceHelper";
    private volatile static FaceHelper instance;
    private int maxFaceNum;

    private FaceDetector detector;

    private FaceHelper() {
        //no instance
        maxFaceNum = 5;

    }

    public static FaceHelper getInstance() {
        if (null == instance) {
            synchronized (FaceHelper.class) {
                if (null == instance) {
                    instance = new FaceHelper();
                }
            }
        }
        return instance;
    }

    public Bitmap decodeBitmap(byte[] data, int width, int height) {
        if (data == null) {
            return null;
        }
        YuvImage image = new YuvImage(data, ImageFormat.NV21, width, height, null);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compressToJpeg(new Rect(0, 0, width, height), 80, stream);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;//必须设置成565,才能进行人脸识别
        Bitmap raw = BitmapFactory.decodeByteArray(stream.toByteArray(),
                0, stream.size(), options);
        try {
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Matrix matrix = new Matrix();

        int  degress =  CameraInterface.getInstance().getPictureDegrees();

        matrix.postRotate(degress);
        //matrix.postRotate(90);

        raw = Bitmap.createBitmap(raw, 0, 0, raw.getWidth(), raw.getHeight(), matrix, false);


      //  Log.e(TAG, "decodeBitmap: "+  raw.getWidth()+":"+raw.getHeight());
        return raw;
    }

    /**
     * @param data
     * @param width
     * @param height
     * @return
     */
    @Nullable
    public Face[] findFaces(byte[] data, int width, int height, Handler handler) {
        if (data == null || data.length < 1) {
            return null;
        }
        Bitmap bitmap = decodeBitmap(data, width, height);
        if (handler != null){
            //处理完成后给handler发送消息
            Message msg = new Message();
            msg.obj = bitmap;
            handler.sendMessage(msg);

        }
        return findFaces(bitmap);
    }




    @Nullable
    public Face[] findFaces(Bitmap source) {
        Log.e(TAG, "findFaces:图片尺寸："+source.getWidth()+","+source.getHeight());
        if (source == null) {
            return null;
        }


        this.detector = new FaceDetector(source.getWidth(),
                source.getHeight(),
                maxFaceNum);
        Face[] faces = new Face[maxFaceNum];
       int detectorFaces = this.detector.findFaces(source, faces);
        Log.e(TAG, "findFaces: "+detectorFaces );

        return faces;
    }




}
